import Vue from 'vue'
import VueRouter from 'vue-router'


// 导入 index.vue
// import index from '../views/index/index.vue'
let index = () => import('../views/index/index.vue')

// 导入index的二级路由
// 导入home.vue
// import home from '../views/home/home.vue'
let home = () => import('../views/home/home.vue')
let cates = () => import('../views/cates/cates.vue')
let my = () => import('../views/my/my.vue')
let shop = () => import('../views/shop/shop.vue')


//一级路由 列表
let list = ()=> import('../views/list/list.vue') 
// 一级路由 详情页
let detail =()=> import('../views/detail/detail.vue')
// 一级路由 登录
let login =()=> import('../views/login/login.vue')
// 一级路由 注册
// let register =()=> import('../views/register/register.vue')
let register = () => import('../views/register/register.vue')
// 一级路由 search
let search =()=> import('../views/search/search.vue')
// 一级路由 settlement
let settlement =()=> import('../views/settlement/settlement.vue')
// 一级路由 地址管理
let address =()=> import('../views/address/address.vue')
// 一级路由 新增地址
let addressNewAdd =()=> import('../views/addressNewAdd/addressNewAdd.vue')
// 一级路由 支付页
let pay =()=> import('../views/pay/pay.vue')
// 全部订单
let allPay =()=> import('../views/allPay/allPay.vue')

Vue.use(VueRouter)




const router = new VueRouter({
  routes: [
    {
      path: "/index",
      component: index,
      children: [
        {
          path: 'home',
          component: home,
          meta:{
            title:"小u商城"
          }
        },
        {
          path: 'cates',
          component: cates,
          meta:{
            title:"商品分类"
          }
        },
        {
          path: 'shop',
          component: shop,
          meta:{
            title:"购物车"
          }
        },
        {
          path: 'my',
          component: my,
          meta:{
            title:"会员中心"
          }
        },
        {
          path: '',
          redirect: 'home'
        }
      ]
    },
    // 
    {
      path:"/list",component:list,
      meta:{
        title:"列表"
      }
    },
    {
      path:"/detail",component:detail,
      meta:{
        title:"商品详情页"
      }
    },
    {
      path:"/login",component:login,
      meta:{
        title:"登录"
      }
    },
    {
      path:"/register",component:register,
      meta:{
        title:"注册"
      }
    },
    {
      path:"/search",component:search,
      meta:{
        title:"商品搜索"
      }
    },
    {
      path:"/settlement",component:settlement,
      meta:{
        title:"确认订单"
      }
    },
    {
      path:"/address",component:address,
      meta:{
        title:"地址管理"
      }
    },
    {
      path:"/addressNewAdd",component:addressNewAdd,
      meta:{
        title:"新增地址"
      }
    },
    {
      path:"/pay",component:pay,
      meta:{
        title:"支付页面"
      }
    },
    {
      path:"/allPay",component:allPay,
      meta:{
        title:"全部订单"
      }
    },
    {
      path: "*",
      redirect: "/index"
    }
  ]
})

// 路由全局守卫 
router.beforeEach((to,from,next)=>{

  if(localStorage.user){
    next()
    return
  }
  if(to.path !== '/login'){
    next('/login')
    return;
  }
  next()
})

export default router
